package com.rzwl.single.server.error;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author : championjing
 * @ClassName: ParamException
 * @Description: msg 为详细描述信息，message为概括信息
 * @Date: 2019/12/24 0024 11:58
 */
@Profile("!my")
@Slf4j
@ControllerAdvice
public class GlobalControllerAdvice {

    /**
     * 全局异常捕捉处理 500
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public JSONObject errorHandler(Exception ex) {
        log.error("e:{}" , ex);
        JSONObject result = new JSONObject();
        result.put("msg", ex.getMessage() );
        result.put("message", "系统错误，请联系管理员");
        return result;
    }
    /**
     * 参数异常捕捉处理 400
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = ParamException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public JSONObject paramHandler(ParamException ex) {
        log.error("e:{}" , ex);
        JSONObject result = new JSONObject();
        result.put("msg", ex.getMsg() );
        result.put("message", "param error" );
        return result;
    }
    /**
     * 权限异常异常捕捉处理
     * @param ex 403
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = ForbidException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public JSONObject paramHandler(ForbidException ex) {
        log.error("e:{}" , ex);
        JSONObject result = new JSONObject();
        result.put("msg", ex.getMsg() );
        result.put("message", "无操作权限" );
        return result;
    }
    /**
     * 实体类定义报错
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public JSONObject argumentHandler(MethodArgumentNotValidException ex) {
        log.error("e:{}" , ex);
        JSONObject result = new JSONObject();
        String msg = ex.getMessage();
        String[] split = msg.split("default message");
        StringBuilder sb = new StringBuilder();
        try {
            sb.append( split[ split.length-2 ] );
            sb.append( split[ split.length-1 ] );
            msg = sb.toString();
        } catch (IndexOutOfBoundsException e) {
            log.warn("检查报错情况：{}", msg);
        }
        log.info("错误信息:{}",ex.getMessage());
        result.put("msg", msg);
        result.put("message", "参数错误");
        return result;
    }

    @ResponseBody
    @ExceptionHandler(value = NotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public JSONObject notFound(NotFoundException e){
        log.error("e:{}" , e);
        JSONObject result = new JSONObject();
        log.info("错误信息:{}",e.getMessage());
        result.put("msg", e.getMessage());
        result.put("message", "参数错误");
        return result;
    }
}
